package com.huangjue.backend.controller;

import com.huangjue.backend.entity.Video;
import com.huangjue.backend.service.VideoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/video")
@CrossOrigin(origins = "*")
public class VideoController {
    @Autowired
    private VideoService videoService;

    @PostMapping("/list")
    public ResponseEntity<Map<String, Object>> getVideosByDemoId(@RequestBody Map<String, Object> request) {
        Map<String, Object> response = new HashMap<>();
        try {
            Integer demoId = null;
            Object demoIdObj = request.get("demoId");
            if (demoIdObj instanceof Integer) {
                demoId = (Integer) demoIdObj;
            } else if (demoIdObj instanceof String) {
                demoId = Integer.valueOf((String) demoIdObj);
            }
            if (demoId == null) {
                response.put("success", false);
                response.put("message", "demoId不能为空");
                return ResponseEntity.badRequest().body(response);
            }
            List<Video> videos = videoService.getVideosWithHomeworkFlagByDemoId(demoId);
            response.put("success", true);
            response.put("message", "获取视频列表成功");
            response.put("data", videos);
            return ResponseEntity.ok(response);
        } catch (Exception e) {
            response.put("success", false);
            response.put("message", "获取视频列表失败: " + e.getMessage());
            return ResponseEntity.internalServerError().body(response);
        }
    }
} 